package com.example.houduan.Controller;

import com.example.houduan.Pojo.DrugCompany;
import com.example.houduan.Service.DrugCompanyService;
import com.example.houduan.Utils.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Controller
@CrossOrigin
public class DrugCompanyController {

    @Autowired
    DrugCompanyService drugCompanyService;

    @RequestMapping("/getAllDrugCompany")
    @ResponseBody
    public List<DrugCompany> getAllDrugCompany(){
        return drugCompanyService.getAllDrugCompany();
    }


    @RequestMapping("/getDrugCompanyPage")
    @ResponseBody
    public PageModel getDrugCompanyPage(@RequestParam int num,int size){
//        System.out.println(drugCompanyService.getDrugCompanyPage(num, size));
        return drugCompanyService.getDrugCompanyPage(num, size);
    }

    @RequestMapping("/deleteDrugCompany")
    @ResponseBody
    public String deleteDrugCompany(@RequestParam Integer company_id){
        if(drugCompanyService.deleteDrugCompany(company_id)==0){
            return "删除失败";
        }else{
            return "删除成功";
        }
    }

    @RequestMapping("/updateDrugCompany")
    @ResponseBody
    public String updateDrugCompany(@RequestBody DrugCompany drugCompany){
        drugCompany.setDc_updatetime(LocalDateTime.now());
        if(drugCompanyService.updateDrugCompany(drugCompany)==0){
            return "更新失败";
        }else{
            return "更新成功";
        }
    }

    @RequestMapping("/addDrugCompany")
    @ResponseBody
    public String addDrugCompany(@RequestBody DrugCompany drugCompany){
        drugCompany.setDc_updatetime(LocalDateTime.now());
        drugCompany.setDc_createtime(LocalDateTime.now());
        if (drugCompanyService.addDrugCompany(drugCompany)==0){
            return "添加失败";
        }else {
            return "添加成功";
        }
    }

    @RequestMapping("/getDrugCompanyByConditions")
    @ResponseBody
    public List<DrugCompany> getDrugCompanyByConditions(@RequestParam(required = false) Integer company_id,String company_name,String city_name){
        System.out.println(111);
        return drugCompanyService.getDrugCompanyByConditions(company_id,company_name,city_name);
    }
}
